package com.tz.online.user.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.tz.online.entity.User;
import com.tz.online.user.dao.IUserDao;
import com.tz.online.util.HibernateCallback;
import com.tz.online.util.HibernateTemplate;

public class UserDaoImpl implements IUserDao {

	private HibernateTemplate template = new HibernateTemplate();
	
	@Override
	public User selectByName(String userName) {
		return (User) template.execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session ses) throws HibernateException {
				return ses.createQuery("from User as u where u.userName = :name")
						.setString("name", userName)
						.uniqueResult();
			}
		});
	}

	@Override
	public void save(User user) {
		//
		template.execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session ses) throws HibernateException {
				ses.save(user);
				return null;
			}
		});
	}

	@Override
	public void update(User user) {
		//
		template.execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session ses) throws HibernateException {
				ses.update(user);
				return null;
			}
		});
	}

}
